<?php
include 'sysco.function.php';

$con = mysql_connect($HOST, $USER, $PASS);
mysql_select_db($DB);


$arrLessOrder = array();
$lessOrders = getLessOrders($MERCHANT, 30);

$prodIds = array();
foreach ($lessOrders as $lessOrder) {
  $prodIds[] = $lessOrder['PRODUCT_ID'];
}
$products = getProductByIds($MERCHANT, $prodIds);

if ($month == 1) {
$_SESSION['from'] = '2012-12-01 03:00:00';
$_SESSION['to'] = '2013-01-01 02:59:00';
} else if ($month == 2) {
$_SESSION['from'] = '2013-01-01 03:00:00';
$_SESSION['to'] = '2013-02-01 02:59:00';
} else if ($month == 3) {
$_SESSION['from'] = '2013-02-01 03:00:00';
$_SESSION['to'] = '2013-03-01 02:59:00';
} else {
  die('no prev month');
}

$prevSales = getLessOrders($MERCHANT, 10000);

$MERCHANTArr = array('lp000051403449' => 'Vino Santo', 'lpLaVigaRwc' => 'La Viga', 'lpMagdaBg' => 'Magda Luna', 'lpAthertonSubsRwc' => 'Atherton Subs', 'lp000051383792' => 'Pronto pitza');
$fp = fopen('reports/Slow-moving_'.date('F', strtotime($_SESSION['to'])).'-'.  str_replace(' ', '-', $MERCHANTArr[$MERCHANT]).'.csv', 'w');
$arr = array('Rank Trend','Item','Code','Price','Units','Trend', 'Revenue', 'Trend');
fputcsv($fp, $arr);

foreach ($lessOrders as $id => $data) {
  $product = $products[$data['PRODUCT_ID']];
  $trend = ''; $trendT = ''; $trendR ='';
  if (isset($prevSales[$data['PRODUCT_ID']])) {
    $trend = $data['TOTAL_SALE_COUNT'] - $prevSales[$data['PRODUCT_ID']]['TOTAL_SALE_COUNT'];
    $trend = ($trend == 0) ? '' : (($trend > 0) ? 'UP' : 'DOWN');
    $trendT = $data['TOTAL'] - $prevSales[$data['PRODUCT_ID']]['TOTAL'];
    $trendT = (floor($trendT) == 0) ? '' : (($trendT > 0) ? 'UP' : 'DOWN');
    $trendR = $data['RANK'] - $prevSales[$data['PRODUCT_ID']]['RANK'];
    $trendR = ($trendR == 0) ? '' : (($trendR < 0) ? 'UP' : 'DOWN');
  }
  
  $tmp = array($trendR, $product['NAME'], $product['CODE'], $data['PRICE'], $data['TOTAL_SALE_COUNT'], $trend, $data['TOTAL'], $trendT);
  fputcsv($fp, $tmp);
}
//fclose($fp);
fputcsv($fp, array(' '));
fputcsv($fp, array(' '));

///////////// no sales items /////////////////////

$arr = array(' ');
fputcsv($fp, $arr);

if ($month == 1) {
  $_SESSION['from'] = '2013-01-01 03:00:00';
  $_SESSION['to'] = '2013-02-01 02:59:00';
} else if ($month == 2) {
  $_SESSION['from'] = '2013-02-01 03:00:00';
  $_SESSION['to'] = '2013-03-01 02:59:00';
} else if ($month == 3) {
  $_SESSION['from'] = '2013-03-01 03:00:00';
  $_SESSION['to'] = '2013-04-01 02:59:00';
} else {
  die('no this month');
}

$noOrdersItems = getNoSoldItems($MERCHANT);
$prodIds = array();
foreach ($noOrdersItems as $noOrdersItem) {
  $prodIds[] = $noOrdersItem['PRODUCT_ID'];
}
$products = getProductByIds($MERCHANT, $prodIds);
$lastOrderDates = getLastOrderDates($MERCHANT, $prodIds); // CHANGE DATE IN FUNTION TILL TO CONSIDER MONTH

if ($month == 1) {
  $_SESSION['from'] = '2012-11-03 03:00:00';
  $_SESSION['to'] = '2013-02-01 02:59:00';
} else if ($month == 2) {
  $_SESSION['from'] = '2012-12-01 03:00:00';
  $_SESSION['to'] = '2013-03-01 02:59:00';
} else if ($month == 3) {
  $_SESSION['from'] = '2013-01-01 03:00:00';
  $_SESSION['to'] = '2013-04-01 02:59:00';
} else {
  die('no prev month');
}

$d90Sales = getItemSalesByIds($MERCHANT, $prodIds);

foreach ($noOrdersItems as $noOrdersItem) {
  $product = $products[$noOrdersItem['PRODUCT_ID']];
  $sale = isset($d90Sales[$noOrdersItem['PRODUCT_ID']]) ? $d90Sales[$noOrdersItem['PRODUCT_ID']]['TOTAL_SALE_COUNT'] : 0;
  $lastOrderDates1 = isset($lastOrderDates[$noOrdersItem['PRODUCT_ID']]) ? $lastOrderDates[$noOrdersItem['PRODUCT_ID']] : 'N/A';
  $tmp = array($product['NAME'], $product['CODE'], $product['PRICESELL'], $sale, $lastOrderDates1);
  fputcsv($fp, $tmp);
} 

fclose($fp);
?>
